﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Touch2Like.SharedLibrary
{
    public sealed class XML
    {
        public static string XMLEncode(string source)
        {
            string result = source;
            result = result.Replace("&", "&amp;"); // do not move this
            result = result.Replace("<", "&lt;");
            result = result.Replace(">", "&gt;");
            result = result.Replace("'", "&apos;");
            result = result.Replace("\"", "&quot;");
            return result;
        }

        public static string XMLDecode(string source)
        {// You probably do not need this...
            string result = source;
            result = result.Replace("&lt;", "<");
            result = result.Replace("&gt;", ">");
            result = result.Replace("&apos;", "'");
            result = result.Replace("&quot;", "\"");
            result = result.Replace("&amp;", "&"); // do not move this
            return result;
        }

        public static string BuildTag(string tagName, string value)
        {
            string result = "<" + XMLEncode(tagName) + ">" + XMLEncode(value) + "</" + XMLEncode(tagName) + ">";
            return result;
        }

        public static string BuildTag(string tagName, object value)
        {
            string result = "<" + XMLEncode(tagName) + ">" + XMLEncode(Strings.Coalesce(value)) + "</" + XMLEncode(tagName) + ">";
            return result;
        }

        public static string BuildTagIfNotEmpty(string tagName, string value)
        {
            string result = "";
            if (value != "")
                result = BuildTag(tagName, value);
            return result;
        }

        public static string BuildTagIfNotEmpty(string tagName, object value)
        {
            string result = "";
            if (value != null)
                if (Strings.Coalesce(value) != "")
                    result = BuildTag(tagName, Strings.Coalesce(value));
            return result;
        }
    }
}
